<?php
/**
 * Created by PhpStorm.
 * User: Silence
 * Date: 2017-07-14
 * Time: 11:53
 */

namespace App\Http\Controllers\Index;


use App\Base\BaseFunc;
use App\Base\TaskState;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class CustomerTaskController extends Controller
{
    public function searchTask()
    {
        $data['taskData'] = DB::table('task')
            ->where('task_customer','=',session('customer_id'))
            ->leftJoin('task_state','task_state_id','=','task_state')
            ->get();
        return view('/index.start',$data);
    }

    public function moreTask($task_id)
    {
        $data['employee'] = DB::table('employee_re_task')
            ->where('task_id','=',$task_id)
            ->leftJoin('employee','employee.employee_id','=','employee_re_task.employee_id')
            ->first();   //获得领取当前任务的人
        $data['evaluationData'] = DB::table('evaluation')
            ->where('task_id','=',$task_id)
            ->leftJoin('customer','customer.customer_id','=','evaluation.customer_id')
            ->get();
        if($data['employee'] == NULL)
        {
            $data['nowTask'] = DB::table('task')
                ->where('task_id','=',$task_id)
                ->first();  //获得当前任务
            $data['employee_name'] = "暂无";
            $data['employee_phone'] = "暂无";
            $data['employee_id'] = "暂无";
            return view('/index.moreTask',$data);
        }
        else
        {
            $data['nowTask'] = DB::table('task')
                ->where('task_id','=',$task_id)
                ->first();  //获得当前任务
            $data['employee'] = DB::table('employee_re_task')
                ->where('task_id','=',$task_id)
                ->leftJoin('employee','employee.employee_id','=','employee_re_task.employee_id')
                ->first();   //获得领取当前任务的人
            $data['employee_id'] = $data['employee']->employee_id;
            $data['employee_name'] = $data['employee']->employee_name;
            $data['employee_phone'] = $data['employee']->employee_phone;
            return view('/index.moreTask',$data);
        }
    }

    public function uploadTask(TaskState $taskState,BaseFunc $baseFunc)
    {
        $input = Input::only('task_name','task_info','task_money','task_phone','task_people');
        $input['task_customer'] = session('customer_id');
        $input['task_area'] = session('customer_area');
        $input['task_time'] = date('Y-m-d H:i:s');
        $input['task_state'] = $taskState->unstate()->task_state_id;
        $task = DB::table('task')->insert($input);
        if($task)
        {
            $baseFunc->setRedirectMessage(true,"您已成功发布任务！",NULL,"/start");
        }
        else
        {
            $baseFunc->setRedirectMessage(false,"您未成功发布任务，请重新发布！",NULL,"/start");
        }
    }

    public function  delTask(BaseFunc $baseFunc)
    {
        $input = Input::only('task_id');
        $data = DB::table('task')
            ->where('task_id','=',$input['task_id'])
            ->delete();
        if($data)
        {
            $baseFunc->setRedirectMessage(true,"删除任务成功！",NULL,"/start");
        }
        else
        {
            $baseFunc->setRedirectMessage(false,"删除任务失败！",NULL,"/start");
        }
    }

    public function addEva(BaseFunc $baseFunc)
    {
        $input = Input::only('task_id','employee_id','evaluation_info');
        $input['evaluation_time'] = date('Y-m-d H:s:i');
        $input['customer_id'] = session('customer_id');
        $data = DB::table('evaluation')->insert($input);
        if($data)
        {
            $baseFunc->setRedirectMessage(true,"评论添加成功！",NULL,NULL);
            return redirect()->back();
        }
        else
        {
            $baseFunc->setRedirectMessage(false,"评论添加失败！",NULL,NULL);
            return redirect()->back();
        }
    }
}